<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 16:02
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Common extends Controller{
	/**
	 * 判断是否登录
	 */
	public function _initialize()
	{
		if(!Session::has('adminuser')){
			$this->redirect('index/index');
		}
		//获取头像
		$img = Db::table('admin')->where('id',Session::get('adminuser.id'))->value('logo');
		$this->assign('image',$img);
		//获取结算个数
		$applyCount = Db::table('apply')->where('status',1)->group('date')->count();;
		$this->assign('applyCount',$applyCount);
		
	}
	/**
	 * 修改管理员头像
	*/
	public function updatelogo()
	{
		if(Request()->file('file')){
			try{
				$file = Request()->file('file');
				$path = ROOT_PATH . 'public' . DS . 'adminuser' .DS. Session::get('adminuser.id'). DS .'images';
				$info = $file->validate(['size'=>1567800])->move($path);
				$imgPath = 'adminuser'. DS .Session::get('adminuser.id'). DS .'images'.DS;
				if($info){
					$input['image'] = $imgPath.$info->getSaveName();
					$result = Db::table('admin')->where('id',Session::get('adminuser.id'))->update(['logo'=>$input['image']]);
					if($result){
						return json($input);
					}else{
						throw new \PDOException('头像更换失败');
					}
				}else{
					throw new \PDOException('头像更换失败');
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
}